<?php
/**
 * 添加affiliate分组
 */
namespace app\controllers\crontab;
use yii;
use app\models\marketing\User;
use app\components\TemplateMarket;
use app\models\marketing\MailGroup;
use app\models\marketing\MailSendlist;
use app\models\marketing\MailGroupEmailTypeStatistics;
use app\models\Elog;
class AffiliateAddMailGroupController extends \app\base\Controller
{
    
    /**
     * 生成分组
     */
    public function actionAddGroupAffiliate()
    {
        ignore_user_abort(true); 
        set_time_limit(0);
        $user = new User();
        
        $userInfo = $user->getAffiliateData();
        if (!empty($userInfo)) {
            $siteInfo = Yii::$app->params['sites']['marketing_email']['web_config']['affiliate'];
            $siteId = $siteInfo['site_id'];
            $templateMarket = new TemplateMarket();
            
            // 查询模板的相关信息
            $templateInfo = $templateMarket->getTemplateInfoByWebId($userInfo['webId'],$siteId);
            if (empty($templateInfo)) { // 说明模板不存在
                
                // 更改当前用户的状态，说明不符合条件
                $user->updateAffiliateUserStatus($userInfo['webId'],3);
                Elog::logCrontab("{$userInfo['webId']}邮件模板不存在!", 'actionAddGroupAffiliate','exception');
                echo "{$userInfo['webId']}邮件模板不存在!";
            }
           
            // 添加分组
            $curBjTime = \yii::$app->bjTime->time();
            $data = [];
            $templateDay = yii::$app->bjTime->date('md',$templateInfo['last_modify']);
            $independentTag = Yii::$app->params['sites']['marketing_email']['web_config']['affiliate']['independent_tag'];
            $data['site_id'] =  $siteId;
            $data['template_id'] = $templateInfo['template_id'];
            $data['template_pk'] = $templateInfo['id'];
            $data['group_name'] = "{$templateInfo['web_id']}_{$templateDay}_{$templateInfo['last_modify']}_{$independentTag}";
            $data['utm_campaign'] = $data['group_name'];
            $data['send_mode'] = 2;
            $data['send_type'] = 0;
            $data['send_type_time'] = 0;
            $data['channel_id'] = 2;
            
            $data['api_email_type'] = $userInfo['userInfo']['api_email_type'];
            $data['state'] = 1;
            $data['start_time'] = $curBjTime;
            $data['boot_time'] = $curBjTime;
            $data['is_use_independent_ip'] = 1;
            if ($userInfo['userInfo']['api_email_type'] == 1) { // 电子
                // $data['sender'] =  Yii::$app->params['sites']['marketing_email']['web_config']['affiliate']['api_email_electronic'];
                $data['sender'] =  Yii::$app->params['sites']['marketing_email']['web_config']['affiliate']['independent_channel_electronic']['emarsys'];
            } else { // 服装
                $data['sender'] =  Yii::$app->params['sites']['marketing_email']['web_config']['affiliate']['independent_channel']['emarsys'];
            }
            $data['is_fix_time_send'] = 0;
            $data['track_code'] = '';
            
            $groupModel = new MailGroup();
            $sendListModel = new MailSendlist();
            $statModel = new MailGroupEmailTypeStatistics();
            $trans = Yii::$app->db->beginTransaction();
            try {
                $groupId = $groupModel->addGroup($data);
               
                $count = $sendListModel->addAffiliateSendListByWebId($userInfo['webId'],$groupId,$data['channel_id'],$data['template_pk']);
                if (0 == $count) {
                    $groupModel->updateGroupByCondition(['group_id' => $groupId], ['is_del' => 1]);
                } else {
                    $groupModel->updateGroupByCondition(['group_id' => $groupId], ['total_count' => $count]);
                    $statModel->addMailStatItem($groupId, $data['site_id']);
                }
                $trans->commit();
            } catch (\Exception $ex) {
                $trans->rollBack();
                Elog::logCrontab("affiliate标识为{$userInfo['webId']}的数据添加分组失败：".$ex->getMessage(), 'actionAddGroupAffiliate','exception');
                
            }
        } else {
            echo '暂时没有适合发送的用户数据';
        }
    }
    
}
